package com.atjava.webspider.logic;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.atjava.jox.io.JOXReader;
import com.atjava.jox.io.JOXWriter;
import com.atjava.webspider.consts.Const;
import com.atjava.webspider.gui.SpiderMainGui;
import com.atjava.webspider.gui.table.DefaultTableModel;
import com.atjava.webspider.model.Task;
import com.atjava.webspider.util.DialogUtil;
import com.atjava.webspider.util.FileSystemUtil;

public class TaskLogic {
	
	private JOXReader jox = new JOXReader();
	
	/*public void startTask(String id)throws Exception{
		Task t = getTask(id);
		startTask(t);
	}
	public void startTask(Task t)throws Exception{
		SpiderTask spider = new SpiderTask(t);
		spider.start();
	}*/
	
	@Deprecated
	public Task getTask1(String id)throws Exception{
		File f = new File(Const.TASK_HOME + "/" + id + "/task.xml");
		Task tsk = jox.readXml(f, Task.class);
		tsk.setId(id);
		return tsk;
	}
	
	public Map<String, List<Task>> getAllTask()throws Exception{
		return getAllTask(new File(Const.TASK_HOME));
	}
	
	public int getTaskIndexById(List<Task> tskList,String id){
		for (int i = 0; i < tskList.size(); i++) {
			Task tsk = tskList.get(i);
			if(id.equals(tsk.getId()))
				return i;
		}
		return -1;
	}
	
	public void updateTaskFile(Task task){
		//更新task
		File taskFile = new File(task.getId());
		JOXWriter writer = new JOXWriter();
		try {
			writer.writeXml(taskFile, task);
		} catch (Exception e) {
			DialogUtil.showError
			(SpiderMainGui.getInstance(), "更新任务数据错误！");
		}
	}
	
	public Map<String, List<Task>> getAllTask(File path)throws Exception{
		List<Task> tskList = new ArrayList<Task>();
		Map<String, List<Task>> taskMap = new HashMap<String, List<Task>>();
		if(".svn".equals(path.getName()))
			return taskMap;
		File [] taskFiles = path.listFiles();
		for (int i = 0; i < taskFiles.length; i++) {
			if(!taskFiles[i].exists())
				continue;
			if(taskFiles[i].isDirectory()){
				Map<String, List<Task>> subTaskMap = getAllTask(taskFiles[i]);
				taskMap.putAll(subTaskMap);
			}else{
				String taskFileName = taskFiles[i].getName();
				if("xml".equalsIgnoreCase(FileSystemUtil.getFileType(taskFileName)) && taskFileName.startsWith("task-")){
					Task tsk = jox.readXml(taskFiles[i], Task.class);
					tsk.setId(taskFiles[i].getAbsolutePath());
					tskList.add(tsk);
				}
			}
		}
		if(tskList.size() > 0)
			taskMap.put(path.getAbsolutePath(), tskList);
		return taskMap;
	}
	

	public static void main(String[] args) throws Exception{

		/*System.out.println(ClassLoader.getSystemResource(""));
		System.out.println(Spider.class.getResource("")); // Test为你自己的类文件
		System.out.println(Spider.class.getResource("/")); // Class文件所在路径
		System.out.println(new File("/").getAbsolutePath());
		System.out.println(System.getProperty("user.dir"));
		System.out.println(Const.APP_CONFIG_PATH);*/
		/*String [][] data = new TaskLogic().loadTaskData();
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				System.out.println(data[i][j]);
			}
		}*/
		System.out.println(new TaskLogic().getAllTask(new File(Const.TASK_HOME)));
	}
}
